binary search data structures dp greedy sortings two pointers *1700

Please click on ads to support us..

Python Code:

import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))
def readInt():
    return int(input())
def readInts():
    return map(int, input().split())
def readStr():
    return input().strip()

def solve():
    n, arr, m = readInt(), readList(), readInt()
    power = [0] * (n+1)
    for _ in range(m):
        p, s = readInts()
        power[s] = max(power[s], p)
    for i in range(n-1, 0, -1):
        power[i] = max(power[i], power[i+1])

    if max(arr) > power[1]:
        return -1

    cnt, cp, ans = 0, 0, 1
    for i in range(n):
        cp = max(cp, arr[i])
        if cp <= power[cnt+1]:
            cnt += 1
        else:
            ans += 1
            cnt, cp = 1, arr[i]
    return ans


for _ in range(int(input())):
    print(solve())


Comments

Submit
0 Comments
More Questions

Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses